<script setup lang="ts">
import {
  lmsSupplierConfigList,
  LibraryOptions,
  supplierOptions,
} from "@/api/DataIntegration/HisQuery‌/index";
import type { RequestParams } from "@/api/DataIntegration/HisQuery‌/type";

import { onMounted, ref } from "vue";
import useUserStore from "@/store/modules/user";
import dayjs from "dayjs";
let useStore = useUserStore();
const RobotList = ref([{ scheduleFlag: 1 }] as any); //列表数据

const queryFormRef = ref(); //refForm
const ids = ref([]); //表格全选
const total = ref();
const loading = ref(false);

let queryParams = ref({
  pageNum: 1,
  pageSize: 10,
} as any);

/**
 * 列表数据
 */
const getRbtList = async () => {
  try {
    const res = await lmsSupplierConfigList(queryParams.value);
    RobotList.value = res.data.records;
    total.value = res.data.total;
    console.log(res.data.records, "分页列表");
    // console.log(queryParams.value);
  } catch (error) {
    // console.error(error);
  }
};
/**
 * 重置
 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  queryParams.value = {} as RequestParams;
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  selectedDateRange.value = [];
  getRbtList();
};
/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

const libraryaOpt = ref([] as any);
const supplierOpt = ref([] as any);
onMounted(async () => {
  getRbtList();
  // libOptions();

  const res = await LibraryOptions(useStore.encryptedData);
  libraryaOpt.value = res.data;
  const ress = await supplierOptions(useStore.encryptedData);
  supplierOpt.value = ress.data;

  //console.log(ress, " ress");
});
const handleSizeChange = (val: number) => {
  queryParams.value.pageSize = val;
  queryParams.value.pageNum = 1;

  getRbtList();
};
const handleCurrentChange = (val: number) => {
  queryParams.value.pageNum = val;
  getRbtList();
  window.scrollTo({ top: 0, behavior: "smooth" }); // 立即回到顶部，并使用平滑滚动效果
};
// 控制序号
const indexMethod = (index: any) => {
  // 1、index:代表当前行数据的下标
  // 2、下标+1:  分页不要纠结：这块不是业务，完全是数学问题！
  return (
    index + 1 + (queryParams.value.pageNum - 1) * queryParams.value.pageSize
  );
};
const selectedDateRange = ref([] as any);
const formatDateTime = () => {
  queryParams.value.startTime = selectedDateRange.value[0]
    ? dayjs(selectedDateRange.value[0]).format("YYYY-MM-DD HH:mm:ss")
    : "";
  queryParams.value.endTime = selectedDateRange.value[1]
    ? dayjs(selectedDateRange.value[1]).format("YYYY-MM-DD HH:mm:ss")
    : "";
};
const shortcuts = [
  {
    text: "上周",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
      return [start, end];
    },
  },
  {
    text: "上个月",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
      return [start, end];
    },
  },
  {
    text: "三个月前",
    value: () => {
      const end = new Date();
      const start = new Date();
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
      return [start, end];
    },
  },
];
// 枚举
const getStatuscronExpressiont = (status: any) => {
  const STATUSES = [
    { id: 1, name: "图书" },
    { id: 2, name: "书架" },
    { id: 3, name: "盘点信息" },
  ];
  const matched = STATUSES.find((item) => item.id === status);
  return matched ? matched.name : "";
};
const successFlagStatus = (status: any) => {
  const STATUSES = [
    { id: 1, name: "成功" },
    { id: 0, name: "失败" },
  ];
  const matched = STATUSES.find((item) => item.id === status);
  return matched ? matched.name : "";
};
</script>

<template>
  <div class="app-container">
    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="LMS供应商">
          <el-select
            v-model="queryParams.supplierId"
            placeholder="请选择LMS供应商"
            style="width: 200px"
          >
            <el-option
              v-for="item in supplierOpt"
              :key="item.value"
              :label="item.supplierName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="图书馆">
          <el-select
            v-model="queryParams.libraryId"
            placeholder="请选择图书馆"
            style="width: 200px"
          >
            <el-option
              v-for="item in libraryaOpt"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            style="width: 350px"
            v-model="selectedDateRange"
            type="datetimerange"
            @change="formatDateTime"
            :shortcuts="shortcuts"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getRbtList">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card>
      <template #header> </template>
      <el-table
        v-loading="loading"
        highlight-current-row
        :data="RobotList"
        :border="true"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="index"
          width="80"
          label="序号"
          :align="'center'"
          :index="indexMethod"
        />
        <el-table-column label="图书馆" prop="libraryName" :align="'center'" />
        <el-table-column
          label="LMS供应商"
          prop="supplierName"
          :align="'center'"
        />

        <el-table-column label="接口" prop="interfaceType" :align="'center'">
          <template #default="{ row }">
            {{ getStatuscronExpressiont(row.interfaceType) }}
          </template>
        </el-table-column>
        <el-table-column label="开始时间" prop="startTime" :align="'center'">
          <template #default="{ row }">
            {{ dayjs(row.startTime).format("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column label="完成时间" prop="endTime" :align="'center'">
          <template #default="{ row }">
            {{ dayjs(row.endTime).format("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column label="是否成功" prop="createTime" :align="'center'">
          <template #default="{ row }">
            {{ successFlagStatus(row.interfaceType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="耗时（秒）"
          prop="timeConsume"
          :align="'center'"
        />
        <el-table-column label="数据条数" prop="dataNum" :align="'center'" />
        <el-table-column
          label="异常信息"
          prop="exceptionMessage"
          :align="'center'"
          show-overflow-tooltip
        />
        <el-table-column
          label="异常日志"
          prop="exceptionLog"
          :align="'center'"
          show-overflow-tooltip
        />
      </el-table>
      <el-pagination
        style="margin-top: 15px"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
* {
  list-style: none;
}

el-select {
  padding-bottom: 100px;
}
.key-value-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
</style>
